// src/api/service/SysAuthService.ts
import graphqlClient from '../../api/GraphqlConfig';
import {
    GET_ROLES,
    CREATE_ROLE,
    GET_AUTHORITIES,
    DELETE_ROLE,
    UPDATE_ROLE,
    BIND_USER_AUTHORITIES,
    BIND_USER_ROLES
} from '../gql/SysAuthGQL';

// 定义数据类型
export interface Authority {
    id: string;
    authorityName: string;
    createTime: string;
    updateTime: string;
}

export interface Role {
    id: string;
    roleName: string;
    createTime: string;
    updateTime: string;
    authorities: Authority[];
}

export interface RoleResponse {
    id: string;
    roleName: string;
    createTime: string;
    updateTime: string;
    authorities: Authority[];
}

export interface RolesResponse {
    roles: Role[];
}

export interface AuthoritiesResponse {
    authorities: Authority[];
}

export interface CreateRoleInput {
    roleName: string;
    authorityIds: string[];
}

export interface UpdateRoleInput {
    id: string;
    roleName?: string;
    authorityIds?: string[];
}

export interface BindUserAuthorityInput {
    userId: string;
    authorityIds: string[];
}

export interface BindUserRoleInput {
    userId: string;
    roleIds: string[];
}

class SysAuthService {
    /**
     * 获取所有角色
     * @returns 角色列表
     */
    async getRoles(): Promise<Role[]> {
        try {
            const result = await graphqlClient.query<{ sysAuth: RolesResponse }>({
                query: GET_ROLES
            });
            return result.data!.sysAuth.roles;
        } catch (error) {
            console.error('获取角色列表失败:', error);
            throw error;
        }
    }

    /**
     * 创建角色
     * @param input 角色信息
     * @returns 创建的角色
     */
    async createRole(input: CreateRoleInput): Promise<Role> {
        try {
            const result = await graphqlClient.mutate<{ sysAuth: { addRole: Role } }>({
                mutation: CREATE_ROLE,
                variables: {
                    roleName: input.roleName,
                    authorityIds: input.authorityIds
                }
            });
            return result.data!.sysAuth.addRole;
        } catch (error) {
            console.error('创建角色失败:', error);
            throw error;
        }
    }

    /**
     * 获取所有权限
     * @returns 权限列表
     */
    async getAuthorities(): Promise<Authority[]> {
        try {
            const result = await graphqlClient.query<{ sysAuth: AuthoritiesResponse }>({
                query: GET_AUTHORITIES
            });
            return result.data!.sysAuth.authorities;
        } catch (error) {
            console.error('获取权限列表失败:', error);
            throw error;
        }
    }

    /**
     * 删除角色
     * @param id 角色ID
     * @returns 删除结果
     */
    async deleteRole(id: string): Promise<boolean> {
        try {
            const result = await graphqlClient.mutate<{ sysAuth: { deleteRole: boolean } }>({
                mutation: DELETE_ROLE,
                variables: {
                    id: id
                }
            });
            return result.data!.sysAuth.deleteRole;
        } catch (error) {
            console.error('删除角色失败:', error);
            throw error;
        }
    }

    /**
     * 更新角色
     * @param input 角色更新信息
     * @returns 更新后的角色
     */
    async updateRole(input: UpdateRoleInput): Promise<Role> {
        try {
            const result = await graphqlClient.mutate<{ sysAuth: { updateRole: Role } }>({
                mutation: UPDATE_ROLE,
                variables: {
                    id: input.id,
                    roleName: input.roleName,
                    authorityIds: input.authorityIds
                }
            });
            return result.data!.sysAuth.updateRole;
        } catch (error) {
            console.error('更新角色失败:', error);
            throw error;
        }
    }

    /**
     * 绑定用户权限
     * @param input 用户权限绑定信息
     * @returns 绑定结果
     */
    async bindUserAuthorities(input: BindUserAuthorityInput): Promise<boolean> {
        try {
            const result = await graphqlClient.mutate<{ sysAuth: { bindUserAuthority: boolean } }>({
                mutation: BIND_USER_AUTHORITIES,
                variables: {
                    userId: input.userId,
                    authorityIds: input.authorityIds
                }
            });
            return result.data!.sysAuth.bindUserAuthority;
        } catch (error) {
            console.error('绑定用户权限失败:', error);
            throw error;
        }
    }

    /**
     * 绑定用户角色
     * @param input 用户角色绑定信息
     * @returns 绑定结果
     */
    async bindUserRoles(input: BindUserRoleInput): Promise<boolean> {
        try {
            const result = await graphqlClient.mutate<{ sysAuth: { bindUserRole: boolean } }>({
                mutation: BIND_USER_ROLES,
                variables: {
                    userId: input.userId,
                    roleIds: input.roleIds
                }
            });
            return result.data!.sysAuth.bindUserRole;
        } catch (error) {
            console.error('绑定用户角色失败:', error);
            throw error;
        }
    }
}

// 创建并导出 SysAuthService 实例
export const sysAuthService = new SysAuthService();
